Make pipeline able to load processor#32514
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
qubvel
left a comment
There was a problem hiding this comment.
The main changes for pipeline code:
- Added loading of
processor(previously only tokenizer, image_processor and feature_extractor) - Added Pipeline class args to control loading
The main changes
- Refactored test signatures to support
processorpass - Changed the way how class names are obtained for processors of the testing tiny models in a pipeline. Instead of getting them from JSON file, the MAPPINGs are used.
I highlighted these changes below, please see the comments
cc @ydshieh for initial review
There was a problem hiding this comment.
For backward compatibility, we can control with Pipeline class if we need to load specific processors/tokenizers. For example, for zero-shot object detection, we will need to load only the processor, and do not need to load image_processor and tokenizer separately. Other legacy pipelines might load only tokenizer and image_processor, even if they have processor class.
There was a problem hiding this comment.
Piggy-backing on the comment above, this is likely something we want to highlight very clearly in each pipeline's documentation
src/transformers/pipelines/base.py
Outdated
There was a problem hiding this comment.
granular control for loading, see comment in the code
There was a problem hiding this comment.
I think this makes sense and I appreciate us being explicit. This repeats what is said above, but this should be extremely clear in the pipelines documentation if possible
There was a problem hiding this comment.
This one is skipped on the main but was not skipped on this branch, so I added a skip rule here
There was a problem hiding this comment.
This one is skipped on the main
Could you explain a bit what this means?
There was a problem hiding this comment.
Do you mean, in the job run's result, it shows it is skipped (on the main branch)?
There was a problem hiding this comment.
Yes, this test was not running on the main branch. One of the problem of current tests: we are testing a set of cases, and if one of the cases (e.g. the first one) falls under skipTest(...) the rest test cases are not running. Probably it's better to return case status, and then aggregate statuses in the main test function running on multiple cases.
There was a problem hiding this comment.
A simple example to reproduce, this test will be skipped
import unittest
class TestCases(unittest.TestCase):
def test_multiple_cases(self):
for i in range(10):
self.run_test(i)
def run_test(self, i):
if i == 0:
self.skipTest("Skip this test")
elif i == 5:
raise ValueError("This test failed")There was a problem hiding this comment.
Llama no longer raises exception for long generation, so I added it to exclude list in the test. This test was skipped on main.
tests/test_pipeline_mixin.py
Outdated
There was a problem hiding this comment.
Previously, tokenizer, image processor and feature extractor class names were taken from JSON file of tiny models, however, there is no *Processor class there. Now MAPPING lists are used to get class names.
There was a problem hiding this comment.
I am not in favor of using MAPPING for anything except processor.
The pipeline testing is highly tighten to the tiny model on the Hub, and we should use the actual classes that are used on the Hub repositories (to avoid any surprising).
For processor, it's OK to use MAPPING, but we can also change the json file's structure for the long term.
There was a problem hiding this comment.
I did it because in the original tiny-models JSON file we do not have separate lists for image processors and feature extractors, I found they are stored in the same list (even processors are sometimes in this list).
tests/test_pipeline_mixin.py
Outdated
There was a problem hiding this comment.
The order of loading is a bit changed in this test, now the model is loaded first to check if it exists, otherwise, the test is skipped.
processor in pipelineprocessor to the pipeline
|
Hi @qubvel . IIRC, that PR is more about "make pipeline able to accept loading processor", but no currently existing pipeline code is changed to use processor yet (even if we specify to load it), right? |
|
@ydshieh yes, you are right, the following PR will add processor to the ZeroShotObjectDetection pipeline. I decided to split it to simplify the review a bit. |
processor to the pipelinepipeline able to load processor
|
Hi @qubvel I have a general question. The process class could load also a single tokenizer, image processor or feature extractor (if not all components are on a Hub repository). For example, from transformers import AutoProcessor
AutoProcessor.from_pretrained("bert-base-uncased")will turn out to be a tokenizer instead of an instance of Therefore, in # Instantiate processor if needed
if isinstance(processor, (str, tuple)):
processor = AutoProcessor.from_pretrained(processor, _from_pipeline=task, **hub_kwargs, **model_kwargs)should have some guard to make sure we really get a WDYT? I will share my thoughts regarding the testing part this afternoon, somehow related to the same consideration. |
continuation of my previous commentOnce we start to have the code using
for testingSimilar consideration to the above. We should test 2 cases:
|
There was a problem hiding this comment.
This one is skipped on the main
Could you explain a bit what this means?
There was a problem hiding this comment.
Do you mean, in the job run's result, it shows it is skipped (on the main branch)?
tests/test_pipeline_mixin.py
Outdated
There was a problem hiding this comment.
I am not in favor of using MAPPING for anything except processor.
The pipeline testing is highly tighten to the tiny model on the Hub, and we should use the actual classes that are used on the Hub repositories (to avoid any surprising).
For processor, it's OK to use MAPPING, but we can also change the json file's structure for the long term.
tests/test_pipeline_mixin.py
Outdated
tests/test_pipeline_mixin.py
Outdated
There was a problem hiding this comment.
It is designed the same way on the main now, according to the comment some instances might not be loaded due to optional dependencies
transformers/tests/test_pipeline_mixin.py
Lines 256 to 267 in af638c4
There was a problem hiding this comment.
Currently on main, this run_pipeline_test only take a single tokenizer_name and a single processor_name, and try to load them. So if a specified processor_name could not be loaded, we can skip it.
However, here in this PR, we try to collect the processors and put them into the dictionary processors. So logically it's better not to skip the test during this collection.
(But in practice, this may not produce any difference)
Yes, you are correct. It is because I treated them (image processor / feature exactor) equally, and later I decided to make I see better now why you need this change. Could you, at least for now, simply use the information from the summary file |
|
@ydshieh Thanks for reviewing, I addressed the comments, please have a look!
This is what I tried to achieve with granular control for each pipeline class by adding |
|
@ydshieh could you please have a look once again? |
ydshieh
left a comment
There was a problem hiding this comment.
Just a few nits but overall good!
Let me know if my comments are clear 🙏
tests/test_pipeline_mixin.py
Outdated
There was a problem hiding this comment.
Currently on main, this run_pipeline_test only take a single tokenizer_name and a single processor_name, and try to load them. So if a specified processor_name could not be loaded, we can skip it.
However, here in this PR, we try to collect the processors and put them into the dictionary processors. So logically it's better not to skip the test during this collection.
(But in practice, this may not produce any difference)
tests/test_pipeline_mixin.py
Outdated
There was a problem hiding this comment.
the first condition is not necessary at this point.
There was a problem hiding this comment.
otherwise we can get an error while doing tiny_model_summary[model_arch_name]
|
Hi @yonigozlan, just a lack of time 🙂 I will address comments this week, and hopefully it can be merged then! |
|
cc @Rocketknight1 as well (please always ping Matt for pipelines 🤗) |
|
Hey! 🤗 Thanks for your contribution to the Before merging this pull request, slow tests CI should be triggered. To enable this:
(For maintainers) The documentation for slow tests CI on PRs is here. |
9b88faa to
e06053d
Compare
|
@Rocketknight1 can you please review the PR, if that aligns with plans for |
|
I'm sorry, I missed that ping somehow! Reviewing now. |
Rocketknight1
left a comment
There was a problem hiding this comment.
Just finished the review, with a few comments.
Overall summary for core maintainers:
- This is a nice PR! It's clean, and although it seems very large, almost all of the changes are boilerplate adding kwargs and fixing spelling mistakes in a lot of files.
- The actual changes you need to look at are
__init__.py,base.pyandtest_pipeline_mixin.py - I made some comments, but they're nits and shouldn't affect any of the PR logic.
cc @LysandreJik for core maintainer review!
| or feature_extractor is not None | ||
| or isinstance(feature_extractor, str) |
There was a problem hiding this comment.
I think this conditional is redundant - if feature_extractor is a str then it will always be not None as well.
| test_cases = [ | ||
| { | ||
| "tokenizer_name": tokenizer_name, | ||
| "image_processor_name": image_processor_name, | ||
| "feature_extractor_name": feature_extractor_name, | ||
| "processor_name": processor_name, | ||
| } | ||
| for tokenizer_name in tokenizer_names | ||
| for image_processor_name in image_processor_names | ||
| for feature_extractor_name in feature_extractor_names | ||
| for processor_name in processor_names | ||
| ] |
There was a problem hiding this comment.
Note for other reviewers: When a model doesn't support one of these preprocessor types, the list of names will be [None], not an empty list. When I first looked at this quadruple-loop I was worried that test_cases would be empty if any of the four name lists was empty, but that should never occur. For safety, we might want to add a check for that, though, since it depends on behaviour in a different function that might be modified later!
There was a problem hiding this comment.
You are right, let's have this check closer to the loop to ensure we always have something to test
6ff4e68
There was a problem hiding this comment.
After this change, you can also remove
if not isinstance(processor_names, (list, tuple)):
processor_names = [processor_names]
(which was there for the same reason)
There was a problem hiding this comment.
I left it there to make sure we do not have str processor_names
LysandreJik
left a comment
There was a problem hiding this comment.
This looks good! I'm mostly concerned about having good and clear documentation and removing unnecessary warnings.
Pipelines are the very first transformers object many users interact with -> ensuring that they're aware of the info they need, without being hammered by warnigns should be an absolute goal of ours.
Thanks for working on this important change!
| The image processor that will be used by the pipeline to preprocess images for the model. This can be a | ||
| model identifier or an actual image processor inheriting from [`BaseImageProcessor`]. | ||
|
|
||
| Image processors are used for Vision models and multi-modal models that require image inputs. Multi-modal | ||
| models will also require a tokenizer to be passed. | ||
|
|
||
| If not provided, the default image processor for the given `model` will be loaded (if it is a string). If | ||
| `model` is not specified or not a string, then the default image processor for `config` is loaded (if it is | ||
| a string). | ||
| processor (`str` or [`ProcessorMixin`], *optional*): | ||
| The processor that will be used by the pipeline to preprocess data for the model. This can be a model | ||
| identifier or an actual processor inheriting from [`ProcessorMixin`]. | ||
|
|
||
| Processors are used for multi-modal models that require multi-modal inputs, for example, a model that | ||
| requires both text and image inputs. | ||
|
|
||
| If not provided, the default processor for the given `model` will be loaded (if it is a string). If `model` | ||
| is not specified or not a string, then the default processor for `config` is loaded (if it is a string). |
There was a problem hiding this comment.
There are now a few overlapping inputs:
- tokenizer
- feature extractor
- image processor
- processor
I believe it would be nice to highlight somewhere visible (like in the documentation above) what attribute is necessary for what: at no point should a user specify all four of them, for example.
There was a problem hiding this comment.
I added a separate Note section to highlight that we should not provide all types of processors at once
e712717 and refer to a specific pipeline in case one would like to provide them explicitly.
For each specific pipeline we have only required processors args in docs section configured with docs decorator. e.g. here
There was a problem hiding this comment.
Also, updated pipeline doc to more relevant one in 6996695
There was a problem hiding this comment.
Piggy-backing on the comment above, this is likely something we want to highlight very clearly in each pipeline's documentation
| warnings.warn( | ||
| f"Processor will be not loaded, because {processor} is not an instance of `ProcessorMixin`. " | ||
| f"Got type `{type(processor)}` instead.", | ||
| UserWarning, | ||
| ) |
There was a problem hiding this comment.
With transformers already having too many warnings, I'd be cautious about the ones we add.
What purpose does this warning serve? Is it sufficiently actionable? Does it concern users (the ones that will see it), or repo owners/creators that have not configured their processors/feature extractors correctly (that will likely not see this warning)?
There was a problem hiding this comment.
Thanks for the questions!
There was a discussion here. The main idea is that AutoProcessor can load not only processors but also basic processing classes, like tokenizer.
Indeed, a misconfiguration in the model-pipeline-processor setup could trigger this and raising a warning + dropping the processor might be sufficient only when the processor isn't needed in the pipeline at all.
However, with granular control, such a case shouldn't occur. Therefore, it seems more appropriate to replace the warning with an error to clearly indicate a misconfiguration. Otherwise, the error will happen later with a less clear message because the processor is None.
src/transformers/pipelines/base.py
Outdated
|
|
||
| # Previously, pipelines support only `tokenizer`, `feature_extractor`, and `image_processor`. | ||
| # As we start adding `processor`, we want to avoid loading processor for some pipelines, that don't required it, | ||
| # because, for example, use `image_processor` and `tokenizer` separately. |
src/transformers/pipelines/base.py
Outdated
There was a problem hiding this comment.
I think this makes sense and I appreciate us being explicit. This repeats what is said above, but this should be extremely clear in the pipelines documentation if possible
Co-authored-by: Lysandre Debut <hi@lysand.re>
Co-authored-by: Lysandre Debut <hi@lysand.re>
|
@LysandreJik thanks for reviewing, I addressed the comment: made clearer docs and removed the warning. Please have a look whenever you have bandwidth. P.S. The test failure looks unrelated. |
LysandreJik
left a comment
There was a problem hiding this comment.
Ok great, thanks for the changes @qubvel!
* Refactor get_test_pipeline * Fixup * Fixing tests * Add processor loading in tests * Restructure processors loading * Add processor to the pipeline * Move model loading on tom of the test * Update `get_test_pipeline` * Fixup * Add class-based flags for loading processors * Change `is_pipeline_test_to_skip` signature * Skip t5 failing test for slow tokenizer * Fixup * Fix copies for T5 * Fix typo * Add try/except for tokenizer loading (kosmos-2 case) * Fixup * Llama not fails for long generation * Revert processor pass in text-generation test * Fix docs * Switch back to json file for image processors and feature extractors * Add processor type check * Remove except for tokenizers * Fix docstring * Fix empty lists for tests * Fixup * Fix load check * Ensure we have non-empty test cases * Update src/transformers/pipelines/__init__.py Co-authored-by: Lysandre Debut <hi@lysand.re> * Update src/transformers/pipelines/base.py Co-authored-by: Lysandre Debut <hi@lysand.re> * Rework comment * Better docs, add note about pipeline components * Change warning to error raise * Fixup * Refine pipeline docs --------- Co-authored-by: Lysandre Debut <hi@lysand.re>
What does this PR do?
Add
processorto thepipelineand refactor tests a bit to support it. Related toFixes # (issue)
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.